package com.example.myworkmangeruse

import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION
import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE
import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import androidx.work.CoroutineWorker
import androidx.work.ForegroundInfo
import androidx.work.WorkManager
import androidx.work.WorkerParameters


/**
 * @author tgw
 * @date 2021/6/28
 * @describe
 */
class DownloadWorker(context: Context, parameters: WorkerParameters) :
    CoroutineWorker(context, parameters) {

    private val notificationManager =
        context.getSystemService(Context.NOTIFICATION_SERVICE) as
                NotificationManager

    override suspend fun doWork(): Result {
//        val inputUrl = inputData.getString(KEY_INPUT_URL)
//            ?: return Result.failure()
//        val outputFile = inputData.getString(KEY_OUTPUT_FILE_NAME)
//            ?: return Result.failure()
        // Mark the Worker as important
        val progress = "Starting Download"

        setForeground(createForegroundInfo(progress))
        download("inputUrl", "outputFile")
        Log.d("TAGtgw DownloadWorker工作doWork", "download:")
        return Result.success()
    }

    private fun download(inputUrl: String, outputFile: String) {
        // Downloads a file and updates bytes read
        // Calls setForegroundInfo() periodically when it needs to update
        // the ongoing Notification
        Log.d("TAGtgw DownloadWorker工作", "download: $inputUrl")
    }
    // Creates an instance of ForegroundInfo which can be used to update the
    // ongoing notification.
    private fun createForegroundInfo(progress: String): ForegroundInfo {
        val channelId = "ChannelId"
        val title = "标题"
//        val cancel = applicationContext.getString(R.string.cancel_download)
        val cancel ="取消"
        // This PendingIntent can be used to cancel the worker
        val intent = WorkManager.getInstance(applicationContext)
            .createCancelPendingIntent(getId())

        // Create a Notification channel if necessary
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createChannel()
        }

        val notification = NotificationCompat.Builder(applicationContext, channelId)
            .setContentTitle(title)
            .setTicker(title)
            .setContentText(progress)
            .setChannelId(channelId)
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setOngoing(true)
            // Add the cancel action to the notification which can
            // be used to cancel the worker
            .addAction(android.R.drawable.ic_delete, cancel, intent)
            .build()
        Log.d("TAGtgw DownloadWorker工作", "createForegroundInfo:")
//        notificationManager.notify(100,notification)
        return ForegroundInfo(
            100, notification,
            FOREGROUND_SERVICE_TYPE_LOCATION or FOREGROUND_SERVICE_TYPE_MICROPHONE
        )
    }

    @RequiresApi(Build.VERSION_CODES.O)
    private fun createChannel() {
        // Create a Notification channel
        var channel = NotificationChannel(
            "ChannelId",
            "foregroundName",
            NotificationManager.IMPORTANCE_HIGH
        )
        notificationManager?.createNotificationChannel(channel)
        Log.d("TAGtgw DownloadWorker工作", "createChannel:")
    }

    companion object {
        const val KEY_INPUT_URL = "KEY_INPUT_URL"
        const val KEY_OUTPUT_FILE_NAME = "KEY_OUTPUT_FILE_NAME"
    }
}